
interface LoadingInfo{
    title:string; //加载内容
    isLoadingSuccess?: boolean;
}

//加载类 用于 显示加载信息
export default class Loading {
    
    loadings:{[key:string]:LoadingInfo} = {};

    current:string;

    constructor(loadings:{[key:string]:LoadingInfo}){

        Object.values(loadings).forEach(info => {
            info.isLoadingSuccess = false;
        })

        this.loadings = loadings;
    }

    //设置当前加载内容
    setCurrent(key:string){
        this.current = key;
    }

    //指定加载完成
    ok(key:string){
        this.loadings[key].isLoadingSuccess = true;
    }

    //返回当前加载内容
    getCurrentInfo():LoadingInfo{
        return this.loadings[this.current];
    }

    //返回进度
    progress(){

        let infos = Object.values(this.loadings);
        let ok = 0;

        for (let index = 0; index < infos.length; index++) {
            if(infos[index].isLoadingSuccess){
                ok+=1;
            }
        }

        return ok / infos.length;

    }

    //是否全部加载完成
    isAllSuccess():boolean{
        let infos = Object.values(this.loadings);
        for (let index = 0; index < infos.length; index++) {
            if(!infos[index].isLoadingSuccess)
                return false;
        }
        return true;
    }

}


